//
//  SISSContractBottomFillInCell.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/16.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractBottomFillInCell.h"
#import "SISSTextView.h"

@interface SISSContractBottomFillInCell ()<UITextViewDelegate>

/// Label
@property (weak, nonatomic) UILabel *label;
/// textView
@property (weak, nonatomic) SISSTextView *textView;
/// contentLabel
@property (weak, nonatomic) UILabel *contentLabel;
/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;


@end
@implementation SISSContractBottomFillInCell

- (void)setIsShow:(BOOL)isShow {
    _isShow = isShow;
    self.textView.hidden = isShow;
    self.contentLabel.hidden = !isShow;
}

- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    self.label.textColor = contractModel.secondTextColor ? SECONDARY_TEXT_COLOR : MAIN_TEXT_COLOR;
    
    if (contractModel.leftStr.length > 0 && [[contractModel.leftStr substringToIndex:1] isEqualToString:@"*"]) {
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:contractModel.leftStr];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,1)];
        self.label.attributedText = attributedStr;
    } else {
        self.label.text = contractModel.leftStr;
    }
    
    if (self.isShow) {
        self.contentLabel.text = contractModel.rightStr;
        WeakSelf
        CGFloat margin = 8.0;
        [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
            make.left.equalTo(weakSelf.label);
            make.top.equalTo(weakSelf.label.mas_bottom).offset(2 * margin);
            make.bottom.equalTo(weakSelf.bottomLine.mas_top).offset(-margin);
        }];
    } else {
        self.textView.placeholder = contractModel.placeHolder;
        self.textView.text = contractModel.rightStr;
    }
    
    if (contractModel.leftStr.length == 0) {
        WeakSelf
        [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(weakSelf.label.mas_bottom).offset(2);
        }];
    }
}


#pragma mark - SISSTextViewDelegate
//- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
//
//    UIView *view = (UIView *)self.nextResponder.nextResponder;
//
//    CGRect rect = [textView convertRect:textView.bounds toView:view.superview];
//
//    if ((SCREEN_HEIGHT - rect.origin.y - 100) < [SISSPublicSingleton shared].keyboardHeight) {
//
//        view.frame = CGRectMake(0, -[SISSPublicSingleton shared].keyboardHeight + SCREEN_HEIGHT - rect.origin.y - 104, SCREEN_WIDTH, SCREEN_HEIGHT);
//
//    }
//    return YES;
//}

- (void)textViewDidChange:(UITextView *)textView {
     self.contractModel.rightStr = textView.text ? textView.text : @"";
}


- (void)textViewDidEndEditing:(SISSTextView *)textView {
    
    self.contractModel.rightStr = textView.text ? textView.text : @"";
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    //在输入过程中 判断加上输入的字符 是否超过限定字数
    NSString *str = [NSString stringWithFormat:@"%@%@",textView.text,text];
    if (str.length > 500) {
        [SISSTools showSVTitle:@"需求说明不能超过500个字" andTime:1 andColor:YES];
        return NO;
    }
    
    return YES;
}




#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.textColor = MAIN_TEXT_COLOR;
    label.font = [UIFont systemFontOfSize:16];
//    label.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:label];
    self.label = label;
    
    SISSTextView *textView = [[SISSTextView alloc] init];
    textView.font = [UIFont systemFontOfSize:16];
    textView.delegate = self;
    textView.textColor = MAIN_TEXT_COLOR;
    textView.layer.borderWidth = 1;
    textView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    textView.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
    [self.contentView addSubview:textView];
    self.textView = textView;
    
    UILabel *contentLabel = [[UILabel alloc] init];
    contentLabel.font = [UIFont systemFontOfSize:16];
    contentLabel.textColor = MAIN_TEXT_COLOR;
    contentLabel.numberOfLines = 0;
    contentLabel.hidden = YES;
    [self.contentView addSubview:contentLabel];
    self.contentLabel = contentLabel;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;

}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    CGFloat textViewHeight = 100.0;
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.contentView).offset(1.5 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
        make.left.equalTo(weakSelf.label);
        make.top.equalTo(weakSelf.label.mas_bottom).offset(2 * margin);
        make.bottom.equalTo(weakSelf.bottomLine.mas_top).offset(-margin);
    }];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
        make.left.equalTo(weakSelf.label);
        make.top.equalTo(weakSelf.label.mas_bottom).offset(2 * margin);
        make.height.mas_equalTo(textViewHeight);
        make.bottom.equalTo(weakSelf.bottomLine.mas_top).offset(-margin);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(weakSelf.textView.mas_bottom).offset(1.5 * margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}


@end
